package domain.entities;

import org.hibernate.LockMode;
import org.hibernate.criterion.CriteriaSpecification;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import java.util.List;

/**
 * A data access object (DAO) providing persistence and search support for
 * MiembroBms domain.entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see domain.entities.MiembroBms
 * @author MyEclipse Persistence Tools
 */

public class MiembroBmsDAO extends HibernateDaoSupport {
	private static final Logger log = LoggerFactory
			.getLogger(MiembroBmsDAO.class);
	// property constants
	public static final String NAME = "name";
	public static final String SURNAME = "surname";
	public static final String EMAIL = "email";
	public static final String PHONE = "phone";
	public static final String MOBILE_PHONE = "mobilePhone";

	protected void initDao() {
		// do nothing
	}

	public void save(MiembroBms transientInstance) {
		log.debug("saving MiembroBms instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(MiembroBms persistentInstance) {
		log.debug("deleting MiembroBms instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public MiembroBms findById(Integer id) {
		log.debug("getting MiembroBms instance with id: " + id);
		try {
			MiembroBms instance = (MiembroBms) getHibernateTemplate().get(
					"domain.entities.MiembroBms", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List<MiembroBms> findByExample(MiembroBms instance) {
		log.debug("finding MiembroBms instance by example");
		try {
			List<MiembroBms> results = (List<MiembroBms>) getHibernateTemplate()
					.findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding MiembroBms instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from MiembroBms as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List<MiembroBms> findByName(Object name) {
		return findByProperty(NAME, name);
	}

	public List<MiembroBms> findBySurname(Object surname) {
		return findByProperty(SURNAME, surname);
	}

	public MiembroBms findByEmail(Object email) {
        List miembros =  findByProperty(EMAIL, email);
        if(miembros.size() > 0)
		    return (MiembroBms) findByProperty(EMAIL, email).toArray()[0];
        return null;
	}

	public List<MiembroBms> findByPhone(Object phone) {
		return findByProperty(PHONE, phone);
	}

	public List<MiembroBms> findByMobilePhone(Object mobilePhone) {
		return findByProperty(MOBILE_PHONE, mobilePhone);
	}

	public List findAll() {
		log.debug("finding all MiembroBms instances");
		try {
			String queryString = "from MiembroBms";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public MiembroBms merge(MiembroBms detachedInstance) {
		log.debug("merging MiembroBms instance");
		try {
			MiembroBms result = (MiembroBms) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(MiembroBms instance) {
		log.debug("attaching dirty MiembroBms instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(MiembroBms instance) {
		log.debug("attaching clean MiembroBms instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static MiembroBmsDAO getFromApplicationContext(ApplicationContext ctx) {
		return (MiembroBmsDAO) ctx.getBean("MiembroBmsDAO");
	}


    public List findByNameRoleId(String name) {
        try {

            String queryString = "select mbp from MiembroBms mbp where name like '%" + name +"%'" +" or surname like '%" + name +"%'" ;
            return getHibernateTemplate().find(queryString);
        } catch (RuntimeException re) {
            log.error("find all failed", re);
            throw re;
        }
    }

    public MiembroBms findByNameSurname(String name, String surname) {
        try {

            String queryString = "select mbp from MiembroBms mbp where name = '" + name.toUpperCase().trim() + "' and surname = '" + surname.toUpperCase().trim() +"'";
            List<MiembroBms>  list =   getHibernateTemplate().find(queryString)  ;
            if(list.size()>0)
               return (MiembroBms) getHibernateTemplate().find(queryString).get(0);
            else
                return null;
        } catch (RuntimeException re) {
            log.error("find all failed", re);
            throw re;
        }
    }
}